package {
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.graphics.SolidColorFill;
	import eDpLib.events.ProxyEvent;
	import flash.events.MouseEvent;

	/**
	 */
	public class GroupWithPivot extends Group {

		public var selectedBox:IsoBox;

		private var _selectedBox_oldX:Number;
		private var _selectedBox_oldY:Number;

		public function GroupWithPivot(grid:IsoGrid, descriptor:Object = null) {
			super(grid, descriptor);

		}

		override public function setMap(map:Array):void {
			super.setMap(map);

			for each (var isoBox:IsoBox in children) {
				isoBox.addEventListener(MouseEvent.CLICK, onBoxClick);
			}
		}

		private function onBoxClick(e:ProxyEvent):void {
			var newBox:IsoBox = e.target as IsoBox;

			if (selectedBox != null) {
				//reset color
				selectedBox.fills = [new SolidColorFill(0x00FFFF, 1), new SolidColorFill(0x8080C0, 1), new SolidColorFill(0xFF8040, 1)];
			}
			if (newBox != selectedBox) {
				//set new color
				newBox.fills = [new SolidColorFill(0x804040, 1), new SolidColorFill(0x400000, 1), new SolidColorFill(0x804040, 1)];
				selectedBox = newBox;
			} else {
				selectedBox = null;
			}

		}

		override public function rotate(clockWise:Boolean = true):void {
			if (selectedBox != null) {
				_selectedBox_oldX = selectedBox.x;
				_selectedBox_oldY = selectedBox.y;
			}

			super.rotate(clockWise);

		}

		override protected function updateChildren():void {
			super.updateChildren();

			if (selectedBox != null) {
				var dx:int = selectedBox.x - _selectedBox_oldX;
				var dy:int = selectedBox.y - _selectedBox_oldY;
				moveTo(x - dx, y - dy, 0);

			}
		}

	}

}
